/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.ui.viewers;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * This template implementation of the {@link ITreePathLabelProvider} interface
 * provides optional callbacks for retrieving and decorating a label image and
 * text.
 */
public class DecoratingWorkbenchTreePathLabelProvider extends LabelProvider
		implements ITreePathLabelProvider {

	/** The is decorating. */
	private boolean isDecorating;

	/** The wb label provider. */
	private ILabelProvider wbLabelProvider;

	/**
	 * Instantiates a new decorating workbench tree path label provider.
	 * 
	 * @param isDecorating
	 *            the is decorating
	 */
	public DecoratingWorkbenchTreePathLabelProvider(boolean isDecorating) {
		this.isDecorating = isDecorating;
		this.wbLabelProvider = WorkbenchLabelProvider
				.getDecoratingWorkbenchLabelProvider();
	}

	/**
	 * Checks if is decorating.
	 * 
	 * @return true, if is decorating
	 */
	public final boolean isDecorating() {
		return isDecorating;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#dispose()
	 */
	@Override
	public void dispose() {
		wbLabelProvider.dispose();
		super.dispose();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
	 */
	@Override
	public final Image getImage(Object element) {
		return getImage(element, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
	 */
	@Override
	public final String getText(Object element) {
		return getText(element, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreePathLabelProvider#updateLabel(org.eclipse
	 * .jface.viewers.ViewerLabel, org.eclipse.jface.viewers.TreePath)
	 */
	public final void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element != null) {
			Object parentElement;
			TreePath parentPath = elementPath.getParentPath();
			if (parentPath != null) {
				parentElement = parentPath.getLastSegment();
			} else {
				parentElement = null;
			}
			label.setImage(getImage(element, parentElement));
			label.setText(getText(element, parentElement));
		}
	}

	/**
	 * Returns the decorated image for the given {@link TreePath} element, it's
	 * parent element.
	 * 
	 * @param element
	 *            the element
	 * @param parentElement
	 *            the parent element
	 * 
	 * @return the image
	 */
	protected Image getImage(Object element, Object parentElement) {
		return wbLabelProvider.getImage(element);
	}

	/**
	 * Returns the decorated text for the given {@link TreePath} element, it's
	 * parent element.
	 * 
	 * @param element
	 *            the element
	 * @param parentElement
	 *            the parent element
	 * 
	 * @return the text
	 */
	protected String getText(Object element, Object parentElement) {
		return wbLabelProvider.getText(element);
	}
}
